{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">商家名称：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="输入商家名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">联系电话：</label>
                        <div class="layui-input-inline mr0">
                            <input name="phone" class="layui-input" type="text" placeholder="输入联系电话"/>
                        </div>
                    </div>
                    <div class="layui-inline ">
                        <label class="layui-form-label">状&emsp;&emsp;态：</label>
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="" selected>所有</option>
                                <option value="0">待审核</option>
                                <option value="1">正常</option>
                                <option value="2">已关闭</option>
                                <option value="3">已驳回</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>

                        <button id="btnAddUser" class="layui-btn icon-btn {:auth('mall.store/add')}"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarUser">
    <a class="layui-btn layui-btn-primary layui-btn-xs {:auth('mall.store/edit')}" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs {:auth('mall.store/delete')}" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs {:auth('mall.store/check')}" lay-event="check">审核 / 预览</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateUser">
    <input type="checkbox" lay-filter="ckStateUser" value="{{d.userId}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.state==0?'checked':''}}/>
</script>
<script type="text/html" id="tableStateUser1">
    <input type="checkbox" lay-filter="ckStateUser1" value="{{d.id}}" lay-skin="switch"
           lay-text="热门|非热门" {{d.is_top==1?'checked':''}}/>
</script>

<!-- 表单弹窗 -->


<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: '{:__url("mall.store/getData")}',
            page: true,
            toolbar: true,
            height: 500,
            cellMinWidth: 100,
            cols: [[ //表头
                {checkbox: true, fixed: true}
                , {field: 'id', title: 'ID', sort: true, width: 80}
                , {field: 'name', title: '商家名称'}
                , {field: 'phone', title: '商家电话'}
                , {field: 'address', title: '商家地址'}
                , {field: 'sort', sort: true, title: '排序'}
                , {field: 'update_time', title: '更新时间', width: 160},
                {
                    title: '状态', templet: function (d) {
                    var strs = ['<span class="layui-badge layui-bg-orange">待审核</span>',
                        '<span class="layui-badge layui-bg-green">运营中</span>',
                        '<span class="layui-badge layui-bg-red">已关闭</span>',
                        '<span class="layui-badge layui-bg-red">已驳回</span>'];
                    return strs[d.status];
                }, align: 'center', width: 100
                }
//                ,{field: 'is_top', sort: true, templet: '#tableStateUser1', title: '热门'}
                , {fixed: 'right', width: 220, align: 'center', toolbar: '#tableBarUser'}
            ]]
        });

        // 添加
        $('#btnAddUser').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });
        // 置顶商家
        form.on('switch(ckStateUser1)', function (obj) {
            layer.load(2);
            $.post('{:url("action")}', {
                id: obj.elem.value,
                is_top: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 2});
                } else {
                    layer.msg(res.msg, {icon: 1});
//                    console.log($(obj.elem).prop('checked'));
//                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });
        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;

            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel("edit?id=" + data.id, '{:__url("mall.store/edit")}');
            } else if (layEvent === 'add') { // 删除
                showEditModel('add', '{:__url("mall.store/add")}');
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.name);
            } else if (layEvent === 'check') { // 删除

                showEditModel("check?id=" + data.id, '{:__url("mall.store/check")}');
            } else if (layEvent === 'reset') { // 重置密码
                resetPsw(data.userId, data.username);
            }
        });

        // 显示表单弹窗
        function showEditModel(mUser, url) {
            layer.full(admin.open({
                type: 2,
                title: (mUser ? '修改' : '添加') + '商家',
                content: (mUser),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
//                    var url = mUser ? '{:__url("mall.store/edit")}' : '{:__url("mall.store/add")}';
                    //mUser &&  mUser.id;

                    // 回显数据
                    form.val('modelUserForm', mUser);
                    // 表单提交事件
                    form.on('submit(modelSubmitUser)', function (data) {
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 1) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            }));
        }

        // 删除
        function doDel(userId, nickName) {

            layer.confirm('确定要删除“' + nickName + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.get('{:__url("mall.store/delete")}', {
                    id: userId
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 1) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        // 修改用户状态
        form.on('switch(ckStateUser)', function (obj) {
            layer.load(2);
            $.get('../../json/ok.json', {
                userId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

    });
</script>

</body>
</html>
